

SMSkills = { }

local listHerbReagent =  {
			"Peacebloom", "Silverleaf", "Earthroot",
			"Mageroyal", "Briarthorn", "Swiftthistle", "Bruiseweed", "Stranglekelp",
			"Wild Steelbloom", "Grave Moss", "Kingsblood", "Liferoot",
			"Fadeleaf", "Goldthorn", "Khadgar's Whiskers", "Wintersbite",
			"Firebloom", "Purple Lotus", "Arthas'Tears", "Sungrass", "Blindweed", "Ghost Mushroom", "Gromsblood",
			"Golden Sansam", "Dreamfoil", "Mountain Silversage", "Plaguebloom", "Icecap",
			"Ragveil", "Terocone", "Ancient Lichen", "Nightmare Vine", "Netherbloom", "Mana Thistle", "Dreaming Glory",
			"Icethorn", "Lichbloom", "Fire Leaf", "Fire Seed", "Tiger Lily", "Adder's Tongue", "Deadnettle", "Goldclover", "Talandra's Rose",
		}

local listPigments = {
		["Alabaster Pigment"] = { "Peacebloom", "Silverleaf", "Earthroot" },
		["Dusky Pigment"] = { "Mageroyal", "Briarthorn", "Swiftthistle", "Bruiseweed", "Stranglekelp"},
		["Verdant Pigment"] = { "Mageroyal", "Briarthorn", "Swiftthistle", "Bruiseweed", "Stranglekelp"},
		["Golden Pigment"] = { "Wild Steelbloom", "Grave Moss", "Kingsblood", "Liferoot" },
		["Burnt Pigment"] = { "Wild Steelbloom", "Grave Moss", "Kingsblood", "Liferoot" },
		["Emerald Pigment"] = { "Fadeleaf", "Goldthorn", "Khadgar's Whiskers", "Wintersbite" },
		["Indigo Pigment"] = { "Fadeleaf", "Goldthorn", "Khadgar's Whiskers", "Wintersbite" },
		["Violet Pigment"] = { "Firebloom", "Purple Lotus", "Arthas'Tears", "Sungrass", "Blindweed", "Ghost Mushroom", "Gromsblood" },
		["Ruby Pigment"] = { "Firebloom", "Purple Lotus", "Arthas'Tears", "Sungrass", "Blindweed", "Ghost Mushroom", "Gromsblood" },
		["Silvery Pigment"] = { "Golden Sansam", "Dreamfoil", "Mountain Silversage", "Plaguebloom", "Icecap" },
		["Sapphire Pigment"] = { "Golden Sansam", "Dreamfoil", "Mountain Silversage", "Plaguebloom", "Icecap" },
		["Nether Pigment"] = { "Ragveil", "Terocone", "Ancient Lichen", "Nightmare Vine", "Netherbloom", "Mana Thistle", "Dreaming Glory" },
		["Ebon Pigment"] = { "Ragveil", "Terocone", "Ancient Lichen", "Nightmare Vine", "Netherbloom", "Mana Thistle", "Dreaming Glory" },
		["Azure Pigment"] = { "Icethorn", "Lichbloom", "Fire Leaf", "Fire Seed", "Tiger Lily", "Adder's Tongue", "Deadnettle", "Goldclover", "Talandra's Rose" },
		["Icy Pigment"] = { "Icethorn", "Lichbloom", "Fire Leaf", "Fire Seed", "Tiger Lily", "Adder's Tongue", "Deadnettle", "Goldclover", "Talandra's Rose" },
	}
local listRarePigments = {
		["Verdant Pigment"] = { "Peacebloom", "Silverleaf", "Earthroot" },
		["Burnt Pigment"] = { "Wild Steelbloom", "Grave Moss", "Kingsblood", "Liferoot" },
		["Indigo Pigment"] = { "Fadeleaf", "Goldthorn", "Khadgar's Whiskers", "Wintersbite" },
		["Ruby Pigment"] = { "Firebloom", "Purple Lotus", "Arthas'Tears", "Sungrass", "Blindweed", "Ghost Mushroom", "Gromsblood" },
		["Sapphire Pigment"] = { "Golden Sansam", "Dreamfoil", "Mountain Silversage", "Plaguebloom", "Icecap" },
		["Ebon Pigment"] = { "Ragveil", "Terocone", "Ancient Lichen", "Nightmare Vine", "Netherbloom", "Mana Thistle", "Dreaming Glory" },
		["Icy Pigment"] = { "Icethorn", "Lichbloom", "Fire Leaf", "Fire Seed", "Tiger Lily", "Adder's Tongue", "Deadnettle", "Goldclover", "Talandra's Rose" },
	}

local function tcontains(table, name)
	for _, val in ipairs(table) do
		if val == name then return true end
	end
	return false
end

local function GetAmountString(copper)
	if not copper then return "nil" end

	local silver = copper / 100;  copper = copper % 100
	local gold = silver / 100;  silver = silver % 100

	local string = ""
	if (copper > 0) then string = (" %dc"):format(copper)..string end
	if (silver > 0) then string = (" %ds"):format(silver)..string end
	if (gold > 0)   then string = (" %dg"):format(gold)..string end

	return string;
--	return ("%dg %ds %dc"):format(copper / 100 / 100, (copper / 100) % 100, copper % 100)
end

local function isHerb(name)
	return tcontains(listHerbReagent, name)
end

local function isPigment(name)
	return listPigments[name] ~= nil
end

local function ShowPigmentOptions(pigment, indent)

	local list = listPigments[pigment]
	if not list then
		SMDEBUG(1, "No pigment: "..pigment.."!")
		return
	end
	local string = ""
	for i=1,#(list) do
		string = list[i]..", "..string
	end

	SMDEBUG(1, indent.."Pigment can be milled from one of: "..string)
end

function SMSkills:FindSkill(search, indent)

	if not (indent) then indent = "" end

	for i=1,GetNumTradeSkills() do
		local skillName, skillType, numAvailable, isExpanded, altVerb  = GetTradeSkillInfo(i);
		if (skillName and skillType ~= "header") then
			if (skillName == search) then
				SMDEBUG(2, indent.."You can craft: "..skillName.." (ID: "..(i or "nil")..")");
				return skillName, i
			end
		end
	end
	return null, -1
end

function SMSkills:CraftDetails(search, indent)

	--TODO: Some sort of endless loop is possible with this - put in a safety

	SMDEBUG(4, "Looking for a match with "..(search or "nil"))
	if not (indent) then indent = "" end

	if (isHerb(search)) then
		SMDEBUG(1, indent..search.." is an herb")
		return
	end
	if (isPigment(search)) then
		ShowPigmentOptions(search, indent)
		return
	end
	local cost, zone = SMItemSource:GetVendorCost(search)
	if (cost) then
		SMDEBUG(1, indent..search.." -Vendor in "..zone.." for "..GetAmountString(cost))
		return
	end

	local skillName, skillID = SMSkills:FindSkill(search, indent)

	indent = indent.."    "
	if (skillName and skillID ~= -1)  then

		local numReagents = GetTradeSkillNumReagents(skillID);
		local totalReagents = 0;
		for i=1, numReagents, 1 do
			local reagentName, reagentTexture, reagentCount, playerReagentCount = GetTradeSkillReagentInfo(skillID, i);
			SMDEBUG(2, indent.."Requires: "..(reagentCount or "nil").." of "..(reagentName or "nil")..".  You have "..(playerReagentCount or "nil"))
			if (reagentCount > playerReagentCount) then
				SMSkills:CraftDetails(reagentName, indent)
			end
		end
	else
		SMDEBUG(2, indent.."Don't know how to make or acquire "..search)
	end

end


-- Only works when skill window is open.
function SMSkills:ListSkills()

	for i=1,GetNumTradeSkills() do
	   local skillName, skillType, numAvailable, isExpanded, altVerb  = GetTradeSkillInfo(i);
	   if (skillName and skillType ~= "header") then
	      DEFAULT_CHAT_FRAME:AddMessage("Found: "..skillName);
	   end
	end

end